package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbOrder;
import com.guigu.qingcheng.pojo.TbReturnOrder;
import com.guigu.qingcheng.pojo.vo.ReturnOrderVo;
import com.guigu.qingcheng.service.TbReturnOrderService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/07/31
*/
@Slf4j
@RestController
@RequestMapping("/return/order")
@Api(tags = "TbReturnOrderController")
public class TbReturnOrderController {
    @Resource
    private TbReturnOrderService tbReturnOrderService;

    @ApiOperation(value = "待处理退货申请")
    @GetMapping("/returnorder/{status}")
    public R returnorder(@PathVariable("status") Integer status) {
        Map returnOrder = tbReturnOrderService.findBystatus(status);
        return R.ok().data("returnOrder",returnOrder.get("sumreturn"));
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {

        TbReturnOrder tbReturnOrder = tbReturnOrderService.findById(id);
        return R.ok().data("tbReturnOrder",tbReturnOrder);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbReturnOrder>  list = tbReturnOrderService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbReturnOrder> pageUtils = tbReturnOrderService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbReturnOrder tbReturnOrder, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbReturnOrder> pageUtils = tbReturnOrderService.pageQueryByAll(pageNum, pageSize, tbReturnOrder);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbReturnOrder tbReturnOrder) {

        boolean b = tbReturnOrderService.saveTbReturnOrder(tbReturnOrder);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbReturnOrder tbReturnOrder) {
        boolean b = tbReturnOrderService.updateTbReturnOrder(tbReturnOrder);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbReturnOrderService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "批量删除信息（主键）")
    @DeleteMapping("/batchDelete/{ids}")
    public R batchDelete(@PathVariable("ids") String ids) {
        boolean b = tbReturnOrderService.batchDelete(ids);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "根据id查询信息 ")
    @GetMapping("/queryByReturnId/{id}")
    public R queryByReturnId(@PathVariable("id") Long id) {
        TbReturnOrder tbReturnOrder = tbReturnOrderService.findById(id);
        TbOrder order = tbReturnOrderService.queryByReturnId(id);

        return R.ok().data("tbReturnOrder",tbReturnOrder).data("order",order);
    }



}
